package com.hfi.my;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author ChangLiang
 * @date 2019/5/16
 */
public class MyMapperProxy<T> implements InvocationHandler {

    private final MySqlSession sqlSession;

    // mybatis中 传入mapperInterface是为了做本地缓存
    // 我们这里并没有使用
    private final Class<T> mapperInterface;


    public MyMapperProxy(MySqlSession sqlSession, Class<T> mapperInterface) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) {
        if (method.getDeclaringClass().getName().equals(EmployeeMapperXml.nameSpace)) {
            String sql = EmployeeMapperXml.methodSqlMapping.get(method.getName());
            System.out.println(String.format("SQL:[%s], parameter:[%s]", sql, args[0]));
            return sqlSession.selectOne(sql, String.valueOf(args[0]));
        }
        return null;
    }
}
